import { timeFormat } from '../../model/utils/common';
import wxCharts from '../../components/wxcharts-min';

export default {
    namespace: 'price',
    state: {
        oilType: '',
        price : ''
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *getOilPrice({ payload }, { put, call, select }) {
            const oilType = wx.getStorageSync('oilType')||92;
            const price = yield put.resolve({
                type: '$request/post',
                payload: {
                    url : '/api/getOilPrice',
                    data: {
                        'province': payload.province,
                        'date': payload.date 
                    }
                }
            });
            yield put({
                type : 'save',
                payload : {
                    oilType,
                    price  : price||{}
                }
            });
            yield put({
                type: 'getPriceHistory',
                payload: {
                    price,
                    date: payload.date,
                    province: payload.province
                }
            });
        },
        *getPriceHistory({ payload }, { put, call, select }) {
            const {province, price,date} = payload;
            const history = yield put.resolve({
                type: '$request/post',
                payload: {
                    url : '/api/getPriceHistory',
                    data: {
                        'province': province
                    }
                }
            });
            console.log('history list:',history);
            let compare = null, flag = false;
            for(var i = history.length -1;i>=0;i--){
                const item = history[i];
                if(!compare){
                    // 找到匹配的一项
                    if(new Date(date).getTime() >=  new Date(item.oilDate)){
                        compare = price;
                    }
                }else if(!flag && (compare.oil92 != item.oil92)){
                    price.reduce0 = (  price.oil0 - item.oil0)/100;
                    price.reduce92 = (price.oil92 -item.oil92 )/100;
                    price.reduce95 = (price.oil95 - item.oil95  )/100;
                    price.reduce98 = (price.oil98 - item.oil98  )/100;
                    flag = true;
                }
            }
            yield put({
                type : 'save',
                payload : {
                    price,
                    history  : history||[]
                }
            });

            yield put({
                type: 'initCanvas',
                payload: {
                    history
                }
            });
        },
        /**
         * 
         */
        *initCanvas({payload},{select}){

            const {oilType,history} = yield select(s => s.price);
            const Type = 'oil'+ oilType;
            new wxCharts({
                canvasId: 'lineCanvas',
                type: 'line',
                categories: history.map(item => timeFormat(new Date(item.oilDate),'YYYY-MM-DD')),
                series: [{
                    name: '时间',
                    data: history.map(item => item[Type]/100),
                }],
                yAxis: {
                    title: '价格',
                    format: function (val) {
                        return val.toFixed(2);
                    },
                    // min: Math.floor(temp.oil0)
                },
                width: 320,
                height: 200
            });
        }
    }
};